from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

from time import sleep
import datetime

# import time
# 女方身份证
# 男方身份证
# 护照号
# 电话
# 不自动关闭浏览器
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(chrome_options)
# 输入信息
pageUrl = "http://chinese-embassy.website/app"
# 中国籍名称
HoldName = "xxx"
# 护照号
PassportNo = "xxx"
# 中国身份证
iDNo = "xxxx"
# 证件信息页图片
piguite = "D:/xxx.pdf"
# 柬籍伴侣身份证
PartnerIDNo = "xxx"
# 联系电话
TelNo = "xxx"


def inputInfo():
    # 输入姓名
    holdName = driver.find_element(By.ID, "姓名")
    holdName.clear()
    holdName.send_keys(HoldName)
    # 图片
    piguites = driver.find_element(By.ID, "证件信息页")
    piguites.send_keys(piguite)
    # 输入中国身份证
    idNo = driver.find_element(By.ID, "中国身份证")
    idNo.clear()
    idNo.send_keys(iDNo)
    # 输入柬籍伴侣身份证
    partnerIDNo = driver.find_element(By.ID, "柬籍伴侣身份证")
    partnerIDNo.clear()
    partnerIDNo.send_keys(PartnerIDNo)
    # 输入联系电话
    telNo = driver.find_element(By.ID, "联系电话")
    telNo.clear()
    telNo.send_keys(TelNo)


def order():
    # 打开网站
    driver.get(pageUrl)
    while True:
        now = datetime.datetime.now().strftime("%H:%M:%S.%f")
        print(now)
        if now >= "11:46:00":
            break
    # 点击下一步
    ok_button = driver.find_element(By.XPATH, "//button[@title='OK']")
    ok_button.click()
    driver.implicitly_wait(2)  # 等待0.1
    holdNo = driver.find_element(By.ID, "证件号")
    holdNo.clear()
    holdNo.send_keys(PassportNo)
    driver.implicitly_wait(0.2)  # 等待0.1
    infoSelect = Select(driver.find_element(By.ID, "业务"))
    infoSelect.select_by_index(3)
    driver.implicitly_wait(0.2)  # 等待0.1
    ok_button = driver.find_element(By.XPATH, "//button[@title='OK']")
    ok_button.click()
    elements = driver.find_elements(By.CLASS_NAME, "daybutton")
    button_texts = []  # 用于存储按钮文本的列表
    for button in elements:
        if button.is_enabled():
            item = button.text.split("(")[0]
            button_texts.append(item)
    curBtnId = button_texts[0]
    selectBtn = driver.find_element(By.ID, curBtnId)
    selectBtn.click()
    driver.implicitly_wait(0.1)  # 等待0.1
    inputInfo()
    driver.implicitly_wait(0.2)  # 等待0.1
    ok_button = driver.find_element(By.XPATH, "//button[@title='OK']")
    ok_button.click()
    driver.implicitly_wait(0.1)  # 等待0.1
    reset_button = driver.find_element(By.XPATH, "//button[@title='RESET']")
    reset_button.click()
    sleep(3)
    driver.quit()


order()
